//
//  LGPatientSportsChartController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/9.
//

#import "LGSportsEvaDetailChartView.h"
#import "LGScrollView.h"

#define viewWidth     (Screen_W - viewPix(32))

#define topMargin      viewPix(63)

#define leftMargin     viewPix(35)+iPadMargin(10)

#define lineHeight     viewPix(35)

#define scrollWidth    (viewWidth -leftMargin - viewPix(15))

#define scrollHeight    (lineHeight*5+viewPix(20))

@interface LGSportsEvaDetailChartView()<UIScrollViewDelegate>
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIButton *weekButton;
/**   */
@property (nonatomic , strong)UIButton *monthButton;
/**   */
@property (nonatomic , strong)UILabel *unitLabel;
/**   */
@property (nonatomic , strong)LGScrollView *scrollView;
/**   */
@property (nonatomic , strong)NSArray *yAxisArray;
/**   */
@property (nonatomic , strong)NSArray *dayArray;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , assign)CGFloat itemWidth;
/**   */
@property (nonatomic , assign)CGFloat minValue;
/**   */
@property (nonatomic , assign)CGFloat maxValue;
/**   */
@property (nonatomic , assign)NSInteger index;
/**   */
@property (nonatomic , assign)NSInteger totalCount;
/**   */
@property (nonatomic , strong)NSMutableArray *cacheArray;

@end

@implementation LGSportsEvaDetailChartView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.itemWidth = scrollWidth/7.0;
        [self addSubview:self.titleLabel];
//        [self addSubview:self.weekButton];
//        [self addSubview:self.monthButton];
        [self addSubview:self.scrollView];
        [self addSubview:self.unitLabel];
    }
    return self;
}

-(void)dayButtonAction:(UIButton *)sender{
    sender.selected = YES;
    NSString *day = @"7";
    if(sender == self.monthButton){
        day = @"30";
        self.weekButton.selected = NO;
    }else{
        self.monthButton.selected = NO;
    }
    if(self.checkDayRequest){
        self.checkDayRequest(day);
    }
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.titleLabel.text = LGNSString(dataDic[@"title"]);
    self.unitLabel.text = LGNSString(dataDic[@"unit"]);
    //清空数值
    for (NSInteger i=0; i<self.cacheArray.count; i++) {
        UIView *view = self.cacheArray[i];
        [view removeFromSuperview];
    }
    [self.cacheArray removeAllObjects];
    if(dataDic.allKeys.count>0){
        //画线
        self.totalCount = [dataDic[@"data"][@"xAxis"] count];
        self.yAxisArray = dataDic[@"data"][@"yAxis"];
        self.dayArray = dataDic[@"data"][@"xAxis"];
        self.dataArray = dataDic[@"data"][@"values"];
        self.scrollView.contentSize = CGSizeMake(self.itemWidth*self.totalCount+viewPix(20), scrollHeight);
    }
}


-(void)setYAxisArray:(NSArray *)yAxisArray{
    _yAxisArray = yAxisArray;
    self.minValue = [[yAxisArray firstObject] floatValue];
    self.maxValue = [[yAxisArray lastObject] floatValue];
    for (NSInteger i=0; i<yAxisArray.count; i++) {
        //Y轴标题
        UILabel *label = [UILabel lableWithText:LGNSString(yAxisArray[yAxisArray.count-1-i]) colorString:@"#5C5C5C" textFont:LGFont(12) textAlignment:NSTextAlignmentCenter lines:1];
        label.frame = CGRectMake(0, topMargin+lineHeight*i, leftMargin, lineHeight);
        [self.cacheArray addObject:label];
        [self addSubview:label];
        //虚线
        CGFloat width = self.itemWidth*self.totalCount;
        width = (width>Screen_W)?width:Screen_W;
        LGDottedLineView *lineView = [[LGDottedLineView alloc]initWithFrame:CGRectMake(0, lineHeight*i, width+viewPix(20), lineHeight)];
        [self.scrollView addSubview:lineView];
        [self.cacheArray addObject:lineView];
    }
}

//创建X轴坐标
-(void)setDayArray:(NSArray *)dayArray{
    _dayArray = dayArray;
    for (NSInteger i=0; i<dayArray.count; i++) {
        UILabel *dayLabel = [UILabel lableWithText:dayArray[i] colorString:@"#333333" textFont:LGFont(12) textAlignment:NSTextAlignmentCenter lines:1];
        dayLabel.frame = CGRectMake(viewPix(10)+self.itemWidth*i, scrollHeight-viewPix(36), self.itemWidth, viewPix(36));
        dayLabel.tag = 1000+i;
        [self.scrollView addSubview:dayLabel];
        [self.cacheArray addObject:dayLabel];
    }
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    NSMutableArray *pointArray = [NSMutableArray array];
    for (NSInteger i=0; i<dataArray.count; i++) {
        NSString *value = LGNSString(dataArray[i]);
        CGFloat pointY = (1-([value floatValue] - self.minValue)/(self.maxValue - self.minValue))*lineHeight*(self.yAxisArray.count-1)+lineHeight/2.0;
        UIButton *button = [UIButton buttonWithImage:@"体重-Normal" selectImage:@"体重-Select"];
        button.bounds = CGRectMake(0, 0, viewPix(20), viewPix(20));
        button.center = CGPointMake(viewPix(10)+self.itemWidth/2.0+self.itemWidth*i, pointY);
        button.tag = 2000+i;
        [self.scrollView addSubview:button];
        [self.cacheArray addObject:button];
//        UILabel *valueLabel = [UILabel lableWithText:value colorString:@"#000000" textFont:LGFont(12) textAlignment:NSTextAlignmentCenter lines:1];
//        valueLabel.tag = 3000+i;
//        [self.scrollView addSubview:valueLabel];
//        [self.cacheArray addObject:valueLabel];
//        [valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.bottom.equalTo(button.mas_top);
//            make.centerX.equalTo(button);
//        }];
//        if([status isEqualToString:@"up"]){
//            valueLabel.textColor = [UIColor colorWithString:@"#DB453A"];
//        }else{
//            valueLabel.textColor = [UIColor colorWithString:@"#000000"];
//        }
        NSValue *point = [NSValue valueWithCGPoint:button.center];
        [pointArray addObject:point];
    }
    [self drawCurveLine:pointArray lineColor:@"#429BFF"];
}

//画线
-(void)drawCurveLine:(NSArray *)arry lineColor:(NSString *)color{
    if(arry.count == 0) return;
    CGPoint startPoint = [[arry objectAtIndex:0] CGPointValue];
    CGPoint lastPoint;
    UIBezierPath *lineBeizer = [UIBezierPath bezierPath];
    [lineBeizer setLineWidth:2.0];
    [lineBeizer moveToPoint:startPoint];

    for (NSInteger i = 0;i < arry.count;i++ ) {
        if (i <arry.count-1 ) {
            CGPoint nowPoint = [[arry objectAtIndex:i] CGPointValue];
            CGPoint futPoint  = [[arry objectAtIndex:i+1] CGPointValue];
            [lineBeizer addCurveToPoint:futPoint controlPoint1:CGPointMake((nowPoint.x+futPoint.x)/2, nowPoint.y) controlPoint2:CGPointMake((nowPoint.x+futPoint.x)/2, futPoint.y)];
            if (i == arry.count-2) {
                [lineBeizer moveToPoint:futPoint];//添加连线
                lastPoint = futPoint;
            }
        }
    }
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = lineBeizer.CGPath;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = [UIColor colorWithString:color].CGColor;
    shapeLayer.lineWidth = 2.0;
    [self.scrollView.layer addSublayer:shapeLayer];
    for(NSInteger i=0; i<arry.count; i++){
        UIButton *button = [self viewWithTag:2000+i];
//        UILabel *valueLabel = [self viewWithTag:3000+i];
        [self.scrollView bringSubviewToFront:button];
//        [self.scrollView bringSubviewToFront:valueLabel];
    }
}


#pragma mark
#pragma mark ====> 创建控件
-(NSMutableArray *)cacheArray{
    if(!_cacheArray){
        _cacheArray = [NSMutableArray array];
    }
    return _cacheArray;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"训练时长" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(12), viewPix(14), Screen_W-viewPix(60), viewPix(22));
    }
    return _titleLabel;
}

-(UILabel *)unitLabel{
    if(!_unitLabel){
        _unitLabel = [UILabel lableWithText:@"分钟" colorString:@"#666666" textFont:LGFont(12) textAlignment:NSTextAlignmentLeft lines:1];
        _unitLabel.frame = CGRectMake(viewPix(10), viewPix(50), viewPix(150), viewPix(17));
    }
    return _unitLabel;
}

-(UIButton *)weekButton{
    if(!_weekButton){
        _weekButton = [UIButton buttonWithTitle:@"7天" titleFont:LGFont(12) normalColor:@"#999999" selectColor:@"#2E9AFC"];
        _weekButton.frame = CGRectMake(viewWidth-viewPix(113), viewPix(12), viewPix(43), viewPix(28));
        [_weekButton setBackgroundColor:[UIColor colorWithString:@"#EFEFEF"] forState:UIControlStateNormal];
        [_weekButton setBackgroundColor:[UIColor colorWithString:@"#EAF4FF"] forState:UIControlStateSelected];
        _weekButton.cornerRidus = viewPix(14);
        _weekButton.selected = YES;
        [_weekButton addTarget:self action:@selector(dayButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _weekButton;
}

-(UIButton *)monthButton{
    if(!_monthButton){
        _monthButton = [UIButton buttonWithTitle:@"30天" titleFont:LGFont(12) normalColor:@"#999999" selectColor:@"#2E9AFC"];
        _monthButton.frame = CGRectMake(viewWidth-viewPix(63), viewPix(12), viewPix(50), viewPix(28));
        [_monthButton setBackgroundColor:[UIColor colorWithString:@"#EFEFEF"] forState:UIControlStateNormal];
        [_monthButton setBackgroundColor:[UIColor colorWithString:@"#EAF4FF"] forState:UIControlStateSelected];
        _monthButton.cornerRidus = viewPix(14);
        [_monthButton addTarget:self action:@selector(dayButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _monthButton;
}

-(LGScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[LGScrollView alloc] initWithFrame:CGRectMake(leftMargin, topMargin, scrollWidth, scrollHeight)];
        _scrollView.contentSize = CGSizeMake(self.itemWidth*7+viewPix(20), scrollHeight);
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
    }
    return _scrollView;
}



@end
